home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / genial / func / call.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-14  |  9.3 KB  |  442 lines

  1.  
  2. /*
  3.  * regions/call.c -- entry points for region selection
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "ui.h"
  8. #include "common.h"
  9. #include "llist.h"
  10. #include "sm.h"
  11. #include "log.h"
  12. #include "reg.h"
  13. #include "display.h"
  14.  
  15. /* type of region for a particular function */
  16. static int rtype = BOX;
  17.  
  18. /* #define TRACE_EX */
  19.  
  20. /***********************************************/
  21. int
  22. getrtype()
  23. {
  24.     return rtype;
  25. }
  26.  
  27. /***********************************************/
  28. setrtype(t)
  29.     int       t;
  30. {
  31.     if (getnpoints() != 0) {
  32.     XBell(display, 0);
  33.     fprintf(stderr, "can not change region types once points have been entered.\n");
  34.     return -1;
  35.     }
  36.     set_region_plim(t);
  37.     return (0);
  38. }
  39.  
  40. /***********************************************/
  41. set_region_plim(t)
  42.     int       t;
  43. {
  44.     rtype = t;
  45.     switch (rtype) {
  46.     case NOREG:
  47.     setplim(0);
  48.     new_state(REG_SEL);
  49.     break;
  50.     case LINE:
  51.     if (line_mode != LSQ)
  52.         setplim(2);
  53.     else
  54.         setplim(MAXPOINTS);
  55.     break;
  56.     case SPLINE:
  57.     setplim(MAXPOINTS);
  58.     break;
  59.     case CLSPLINE:
  60.     setplim(MAXPOINTS);
  61.     break;
  62.     case POLYGON:
  63.     setplim(MAXPOINTS);
  64.     break;
  65.     case BOX:
  66.     setplim(2);
  67.     break;
  68.     case DUBLIN:
  69.     setplim(4);        /* 2 endpoints for 2 lines */
  70.     break;
  71.     case AN_TEXT:
  72.     setplim(1);        /* just one point -- the start location */
  73.     break;
  74.     case AN_VEC:
  75.     setplim(2);
  76.     break;
  77.     case ENTIRE_IMAGE:
  78.     setplim(0);
  79.     new_state(REG_SEL);
  80.     break;
  81.     default:
  82.     printf("Unknown region type!\n");
  83.     break;
  84.     };
  85.     reg_setup();
  86.     return 0;
  87. }
  88.  
  89. /*****************************************************************/
  90. /* reg_setup() -- setup stuff for mode REG_SEL. */
  91. reg_setup()
  92. {
  93.     pvreg_start();
  94. }
  95.  
  96. /*****************************************************************/
  97.  
  98. /* initializes the current region */
  99. /* the current region is used to "preview" the region before passing it the
  100.  function with the eval button */
  101. pvreg_start()
  102. {
  103. #ifdef TRACE_EX
  104.     fprintf(stderr, "in pvreg_start \n");
  105. #endif
  106.  
  107.     if (curfunc->pvreg != NULL) {
  108.     free(curfunc->pvreg);
  109.     }
  110.     curfunc->pvreg = newreg(rtype);
  111. }
  112.  
  113. pvreg_set(head)
  114.     struct plist *head;
  115. {
  116. #ifdef TRACE_EX
  117.     fprintf(stderr, "in pvreg_set \n");
  118. #endif
  119.  
  120.     refresh_reg(curfunc->pvreg);/* un-draw any old point */
  121.  
  122.     /* initialize dlist and plist */
  123.     while (curfunc->pvreg->r_dlist != NULL) {
  124.     llist_free((llist **) & curfunc->pvreg->r_dlist);
  125.     }
  126.     if ((curfunc->pvreg->r_dlist =
  127.      (struct dlist *) malloc(sizeof(struct dlist))) == NULL) {
  128.     perror("malloc");
  129.     exit(1);
  130.     }
  131.     curfunc->pvreg->r_dlist->next = curfunc->pvreg->r_dlist->prev = NULL;
  132.     curfunc->pvreg->r_dlist->len = curfunc->pvreg->r_plen = 0;
  133.     curfunc->pvreg->r_plist = head;
  134.  
  135.     draw_cpl();            /* draw all cross-bars in the region */
  136.  
  137. #ifdef OLD
  138.     if (getnpoints() < getplim())
  139.     if (curfunc->pvreg->r_type != AN_TEXT && getnpoints() < 2) 
  140.     return;
  141. #endif
  142.     if ((curfunc->pvreg->r_type == AN_TEXT && getnpoints() < 1) || 
  143.         (curfunc->pvreg->r_type != AN_TEXT && getnpoints() < 2)) 
  144.     return;
  145.  
  146.     if (line_mode == LSQ)
  147.     curfunc->pvreg->r_flags = LSQ;
  148.  
  149.     /* if we have enough points, go ahead and draw the region */
  150.     switch (curfunc->pvreg->r_type) {
  151.     case LINE:
  152.     case DUBLIN:
  153.     draw_line(curfunc->pvreg);
  154.     break;
  155.     case SPLINE:
  156.     draw_spline(curfunc->pvreg);
  157.     break;
  158.     case CLSPLINE:
  159.     draw_clspline(curfunc->pvreg);
  160.     break;
  161.     case POLYGON:
  162.     draw_polygon(curfunc->pvreg);
  163.     break;
  164.     case BOX:
  165.     draw_box(curfunc->pvreg);
  166.     break;
  167.     case AN_TEXT:
  168.     draw_text(curfunc->pvreg);
  169.     break;
  170.     case AN_VEC:
  171.     draw_vec(curfunc->pvreg);
  172.     }
  173.  
  174.     show_region_info();
  175.  
  176. }
  177.  
  178. /***************************************************************/
  179. show_region_info()
  180. {
  181.     char     *mesg, *line_info(), *spline_info(), *clspline_info(), *polygon_info(), *box_info();
  182.  
  183.     switch (curfunc->pvreg->r_type) {
  184.     case LINE:
  185.     case DUBLIN:
  186.     mesg = line_info(curfunc->pvreg);    /* line length */
  187.     break;
  188.     case SPLINE:
  189.     case CLSPLINE:
  190.     mesg = spline_info(curfunc->pvreg);    /* width and height */
  191.     break;
  192.     case POLYGON:
  193.     mesg = polygon_info(curfunc->pvreg);    /* width and height (convex
  194.                          * poly only) */
  195.     break;
  196.     case BOX:
  197.     mesg = box_info(curfunc->pvreg);    /* width and heigth */
  198.     break;
  199.     case AN_TEXT:
  200.     mesg = NULL;
  201.     break;
  202.     case AN_VEC:
  203.     mesg = NULL;
  204.     break;
  205.     }
  206.  
  207.     xv_set(base_win->infomesg, PANEL_LABEL_STRING, mesg, NULL);
  208.     panel_paint(base_win->infomesg, PANEL_CLEAR);
  209. }
  210.  
  211.  
  212. /***********************************************/
  213. struct region *
  214. interp_reg()
  215. {
  216.     struct region *reg;
  217.  
  218. #ifdef TRACE_EX
  219.     fprintf(stderr, "in interp_reg \n");
  220. #endif
  221.  
  222.     reg = newreg(rtype);
  223.     if (get_plist(reg) < 0) {
  224.     return NULL;
  225.     }
  226.     /* free the preview region */
  227.     /*
  228.      * set the r_plist member to NULL 'cos we just copied that to our current
  229.      * region
  230.      */
  231.     if (curfunc->pvreg != NULL) {
  232.     curfunc->pvreg->r_plist = NULL;
  233.     curfunc->pvreg->r_plen = 0;
  234.     free_reg(curfunc->pvreg);
  235.     curfunc->pvreg = NULL;
  236.     }
  237.     if (line_mode == LSQ)
  238.     reg->r_flags = LSQ;
  239.     /* and finally, draw the region... */
  240.     if (build_dreg(reg) < 0)
  241.     return NULL;
  242.     else
  243.     return reg;
  244. }
  245.  
  246. /***********************************************/
  247. /* get the plist from the input */
  248. get_plist(reg)
  249.     struct region *reg;
  250. {
  251. #ifdef TRACE_EX
  252.     fprintf(stderr, "in get_plist \n");
  253. #endif
  254.  
  255. #ifdef OLD
  256.     if (getnpoints() < getplim()) {
  257.     if (curfunc->pvreg->r_type != AN_TEXT && getnpoints() < 2)  {
  258. #else
  259.     if ((curfunc->pvreg->r_type == AN_TEXT && getnpoints() < 1) || 
  260.         (curfunc->pvreg->r_type != AN_TEXT && getnpoints() < 2)) { 
  261. #endif
  262.     /* not enough points to draw a region! */
  263.     free(reg);
  264.     return -1;
  265.     }
  266.     flush_cpl(reg);        /* get the current point list */
  267.  
  268.     return 1;
  269. }
  270.  
  271. /***********************************************/
  272. /* make dlists from plists */
  273. build_dreg(reg)
  274.     struct region *reg;
  275. {
  276.     int       rval = 0;
  277.  
  278. #ifdef TRACE_EX
  279.     fprintf(stderr, "in build_dreg \n");
  280. #endif
  281.  
  282.     switch (reg->r_type) {
  283.     case LINE:
  284.     case DUBLIN:
  285.     draw_line(reg);
  286.     break;
  287.     case SPLINE:
  288.     draw_spline(reg);
  289.     break;
  290.     case CLSPLINE:
  291.     draw_clspline(reg);
  292.     break;
  293.     case POLYGON:
  294.     draw_polygon(reg);
  295.     break;
  296.     case BOX:
  297.     draw_box(reg);
  298.     break;
  299.     case AN_TEXT:
  300.     draw_text(reg);
  301.     break;
  302.     case AN_VEC:
  303.     draw_vec(reg);
  304.     break;
  305.     }
  306.     return rval;
  307. }
  308.  
  309. /***********************************************/
  310. /* refresh_reg() takes a region, UN-draws it */
  311. refresh_reg(reg)
  312.     struct region *reg;
  313. {
  314.     int       wth, hgt, blank;
  315.     char     *get_string(), *text;
  316.  
  317. #ifdef TRACE_EX
  318.     fprintf(stderr, "in refresh_reg \n");
  319. #endif
  320.  
  321.     if (reg != NULL) {
  322.     if (reg->r_sbs != NULL) {
  323.         if (reg->r_sbs->string == NULL) {
  324.         /* not yet copied to region structure, so  get from global in text.c */
  325.         text = get_string(); 
  326.         XTextExtents(xfs, text, strlen(text), &blank, &blank,
  327.                  &blank, ®->r_sbs->metr);
  328.         }
  329.         wth = reg->r_sbs->metr.width + 1;
  330.         hgt = reg->r_sbs->metr.ascent + reg->r_sbs->metr.descent + 4;
  331.         fprintf(stderr, "refreshing image: box size: (%d x %d) \n", wth, hgt);
  332.         XPutImage(display, img_win->d_xid, gc, orig_ximg,
  333.               reg->r_sbs->x, reg->r_sbs->y - 13,    /* source */
  334.               reg->r_sbs->x, reg->r_sbs->y - 13,    /* dest */
  335.               wth, hgt);
  336.     }
  337.     if (reg->r_plist != NULL)
  338.         ref_pl(reg->r_plist, img_win);
  339.     if (reg->r_dlist != NULL)
  340.         ref_dlist(img_win->d_xid, reg->r_dlist);
  341.     }
  342. }
  343.  
  344. /***********************************************/
  345. /*free_reg() frees a region and any associated storage */
  346. free_reg(reg)
  347.     struct region *reg;
  348. {
  349. #ifdef TRACE_EX
  350.     fprintf(stderr, "in free_reg \n");
  351. #endif
  352.  
  353.     if (reg != NULL) {
  354.     if (reg->r_sbs != NULL) {
  355.         free(reg->r_sbs);
  356.         reg->r_sbs = NULL;
  357.     }
  358.     if (reg->r_plist != NULL)
  359.         llist_free((llist **) & reg->r_plist);
  360.     while (reg->r_dlist != NULL) {
  361.         llist_free((llist **) & reg->r_dlist);
  362.     }
  363.     free(reg);
  364.     }
  365. }
  366.  
  367. /* newreg() allocates a new region and returns a pointer to it */
  368. struct region
  369.          *
  370. newreg(type)
  371.     int       type;
  372. {
  373.     struct region *reg;
  374.  
  375. #ifdef TRACE_EX
  376.     fprintf(stderr, "in newreg \n");
  377. #endif
  378.  
  379.     if ((reg = (struct region *) malloc(sizeof(struct region))) == NULL) {
  380.     perror("malloc");
  381.     exit(1);
  382.     }
  383.     if ((reg->r_dlist = (struct dlist *) malloc(sizeof(struct dlist))) == NULL) {
  384.     perror("malloc");
  385.     exit(1);
  386.     }
  387.     if ((reg->r_plist = (struct plist *)
  388.      malloc(sizeof(struct plist))) == NULL) {
  389.     perror("malloc");
  390.     exit(1);
  391.     }
  392.     reg->r_plist = NULL;
  393.     reg->r_plen = 0;
  394.     reg->r_sbs = NULL;
  395.     reg->r_dlist->len = 0;
  396.     reg->r_dlist->prev = reg->r_dlist->next = NULL;
  397.     reg->r_dlist->flags = 0;
  398.     reg->r_type = type;
  399.     reg->r_flags = NULL;
  400.     return reg;
  401. }
  402.  
  403. /***********************************************/
  404. /* reg_addpt() -- add a point to the plist of a region */
  405. reg_addpt(reg, x, y)
  406.     struct region *reg;
  407.     int       x, y;
  408. {
  409.     struct plist *t;
  410.  
  411. #ifdef TRACE_EX
  412.     fprintf(stderr, "in reg_addpt \n");
  413. #endif
  414.  
  415.     t = (struct plist *) malloc(sizeof(struct plist));
  416.     t->pt.x = x;
  417.     t->pt.y = y;
  418.     cbget(orig_ximg, &t->cb, t->pt);
  419.     llist_add((llist *) t, (llist **) & reg->r_plist, (llist **) NULL);
  420. }
  421.  
  422. /*********************************************************/
  423. /* draw_reg() -- draw a region in the standout color on the display. */
  424. draw_reg(reg)
  425.     struct region *reg;
  426. {
  427. #ifdef TRACE_EX
  428.     fprintf(stderr, "in draw_reg \n");
  429. #endif
  430.  
  431.     if (reg != NULL) {
  432.     if (reg->r_plist != NULL && !clean_mode)
  433.         draw_pl(reg->r_plist, img_win);
  434.     if (reg->r_dlist != NULL) {
  435.         draw_dlist(img_win->d_xid, reg->r_dlist);
  436.     }
  437.     if (reg->r_sbs != NULL) {
  438.         draw_text_string(reg);
  439.     }
  440.     }
  441. }
  442.